package edu.umn.se.trap.logicChecker;

import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import edu.umn.se.trap.Day;
import edu.umn.se.trap.Form;
import edu.umn.se.trap.Grant;
import edu.umn.se.trap.Incidental;
import edu.umn.se.trap.Lodging;
import edu.umn.se.trap.Meal;
import databases.GrantDB;
import databases.KeyNotFoundException;
import databases.GrantDB.GRANT_FIELDS;
import edu.umn.se.trap.exceptions.TrapException;

public class CheckLogicForeignGov implements Observer {

    @Override
    /**
     * Update for checking logic for foreign gov
     */
    public void update(Observable o, Object arg) 
    {        
        LogicChecker l1 = (LogicChecker) o;
        Form form = (Form) arg;
        GrantDB grantDB = new GrantDB();
        
        Vector<Grant> grants = form.getGrants();
        int count = 0;
        for (int i = 0; i < grants.size(); i++)
        {
            Grant grant = grants.elementAt(i);
            List<Object> grantInfo;
            
            try 
            {
                grantInfo = grantDB.getGrantInfo(String.valueOf(grant.getAccountNumber()));
                int index = GRANT_FIELDS.FUNDING_ORGANIZATION.ordinal();
                String fundingOrg = (String) grantInfo.get(index);
                if (fundingOrg.equalsIgnoreCase("foreign"))
                {
                    count++;
                }
            } 
            catch (KeyNotFoundException e) 
            {
                l1.setException(new TrapException("Key Invalid - ForeignGov"));
            }
        }
        
        // if all listed grants are of foreign origin
        if (count == grants.size())
        {
            Vector<Day> days = form.getDays();
            for (int i = 0; i < days.size(); i++)
            {
                Day today = days.get(i);
                Meal breakfast = today.getBreakfast();
                Meal lunch = today.getLunch();
                Meal dinner = today.getDinner();
                Lodging lodging = today.getLodging();
                Incidental incidental = today.getIncidentals();
                
                if ((!breakfast.getCountry().equals("") && (breakfast.getCountry().equalsIgnoreCase("USA") || breakfast.getCountry().equalsIgnoreCase("United States"))) || 
                        (!lunch.getCountry().equals("") && (lunch.getCountry().equalsIgnoreCase("USA") || lunch.getCountry().equalsIgnoreCase("United States"))) ||
                        (!dinner.getCountry().equals("") && (dinner.getCountry().equalsIgnoreCase("USA") || dinner.getCountry().equalsIgnoreCase("United States"))) ||
                        (!lodging.getCountry().equals("") && (lodging.getCountry().equalsIgnoreCase("USA") || lodging.getCountry().equalsIgnoreCase("United States"))) ||
                        (!incidental.getCountry().equals("") && (incidental.getCountry().equalsIgnoreCase("USA") || lodging.getCountry().equalsIgnoreCase("United States"))))
                {
                    l1.setException(new TrapException("Foreign Grant money must be spent outside the United States"));
                }
            }
        }
    }
}